package com.zz.admin.authMenuManagement.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zz.admin.authMenuManagement.dto.AuthMenuDto;
import com.zz.admin.authMenuManagement.entity.AuthMenu;
import com.zz.admin.authMenuManagement.mapper.AuthMenuMapper;
import com.zz.admin.authMenuManagement.service.AuthMenuService;
import com.zz.common.base.dto.PageListResultDto;
import com.zz.common.base.enums.BaseExceptionEnum;
import com.zz.common.base.param.QueryParam;
import com.zz.common.constants.BaseConstant;
import com.zz.common.exception.ExceptionFactory;
import com.zz.common.utils.common.UUIDGenerator;
import com.zz.common.utils.mapper.BeanMapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 菜单 服务实现类
 *
 * @author zl
 * @since 2025-11-16 20:04:12
 */
@Service
@Slf4j
public class AuthMenuServiceImpl extends ServiceImpl<AuthMenuMapper, AuthMenu> implements AuthMenuService {

    @Resource
    private AuthMenuMapper authMenuMapper;

    @Override
    public PageListResultDto<AuthMenuDto> queryWithPage(AuthMenuDto authMenuDto, QueryParam queryParam) {
        Page<AuthMenu> page = new Page<>(queryParam.getPageNum(), queryParam.getPageSize());
        QueryWrapper<AuthMenu> wrapper = new QueryWrapper<>();
        wrapper.eq("del_flag", BaseConstant.NO);
        if (StringUtils.isNotBlank(authMenuDto.getMenuName())) {
            wrapper.like("menu_name", "%" + authMenuDto.getMenuName() + "%");
        }
        if (StringUtils.isNotBlank(authMenuDto.getMenuType())) {
            wrapper.eq("menu_type", authMenuDto.getMenuType());
        }
        if (StringUtils.isNotBlank(authMenuDto.getDescription())) {
            wrapper.eq("description", authMenuDto.getDescription());
        }
        if (StringUtils.isNotBlank(authMenuDto.getPath())) {
            wrapper.eq("path", authMenuDto.getPath());
        }
        if (StringUtils.isNotBlank(authMenuDto.getUrl())) {
            wrapper.eq("url", authMenuDto.getUrl());
        }
        if (StringUtils.isNotBlank(authMenuDto.getPermission())) {
            wrapper.eq("permission", authMenuDto.getPermission());
        }
        if (StringUtils.isNotBlank(authMenuDto.getIcon())) {
            wrapper.eq("icon", authMenuDto.getIcon());
        }
        if (StringUtils.isNotBlank(authMenuDto.getTitle())) {
            wrapper.eq("title", authMenuDto.getTitle());
        }
        if (StringUtils.isNotBlank(authMenuDto.getTitleKey())) {
            wrapper.eq("title_key", authMenuDto.getTitleKey());
        }
        if (ObjectUtil.isNotEmpty(authMenuDto.getDisplaySeq())) {
            wrapper.eq("display_seq", authMenuDto.getDisplaySeq());
        }
        if (StringUtils.isNotBlank(authMenuDto.getRecordLog())) {
            wrapper.eq("record_log", authMenuDto.getRecordLog());
        }
        if (StringUtils.isNotBlank(authMenuDto.getUseSys())) {
            wrapper.eq("use_sys", authMenuDto.getUseSys());
        }
        if (StringUtils.isNotBlank(authMenuDto.getLink())) {
            wrapper.eq("link", authMenuDto.getLink());
        }
        if (StringUtils.isNotBlank(authMenuDto.getVisable())) {
            wrapper.eq("visable", authMenuDto.getVisable());
        }
        if (StringUtils.isNotBlank(authMenuDto.getStatus())) {
            wrapper.eq("status", authMenuDto.getStatus());
        }
        page = authMenuMapper.selectPage(page, wrapper);
        List<AuthMenu> authMenus = authMenuMapper.selectList(page, wrapper);
        List<AuthMenuDto> authMenuDtos = BeanMapper.mapList(authMenus, AuthMenu.class, AuthMenuDto.class);
        return new PageListResultDto<>((int) page.getCurrent(), (int) page.getTotal(), queryParam.getPageSize(), authMenuDtos);
    }

    @Override
    public List<AuthMenuDto> queryWithNoPage(AuthMenuDto authMenuDto) {
        QueryWrapper<AuthMenu> wrapper = new QueryWrapper<>();
        wrapper.eq("del_flag", BaseConstant.NO);
        List<AuthMenu> authMenus = authMenuMapper.selectList(wrapper);
        return BeanMapper.mapList(authMenus, AuthMenu.class, AuthMenuDto.class);
    }

    @Override
    public AuthMenuDto queryById(String id) {
        if (StringUtils.isBlank(id)) {
            return null;
        }
        AuthMenu authMenu = authMenuMapper.selectById(id);
        if (authMenu == null || BaseConstant.YES.equals(authMenu.getDelFlag())) {
            return null;
        }
        return BeanMapper.map(authMenu, AuthMenuDto.class);
    }


    @Override
    public List<AuthMenuDto> queryByIds(List<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return new ArrayList<>();
        }
        List<AuthMenu> authMenus = authMenuMapper.selectByIds(ids);
        if (CollectionUtils.isEmpty(authMenus)) {
            return new ArrayList<>();
        }
        return BeanMapper.mapList(authMenus, AuthMenu.class, AuthMenuDto.class);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public AuthMenuDto save(AuthMenuDto authMenuDto) {
        // 根据业务修改
        return insert(authMenuDto);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public List<AuthMenuDto> saveBatch(List<AuthMenuDto> authMenuDtos) {
        // 根据业务修改
        return insertList(authMenuDtos);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public AuthMenuDto modify(AuthMenuDto authMenuDto) {
        // 根据业务修改
        updateById(authMenuDto);
        return authMenuDto;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public List<AuthMenuDto> modifyBatch(List<AuthMenuDto> authMenuDtos) {
        // 根据业务修改
        return updateList(authMenuDtos);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int trueDelete(String id) {
        AuthMenu entity = new AuthMenu();
        entity.setMenuId(id);
        return authMenuMapper.deleteById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(String id) {
        // 根据业务修改
        AuthMenuDto authMenuDto = new AuthMenuDto();
        authMenuDto.setMenuId(id);
        deleteById(authMenuDto);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteBatch(List<String> ids) {
        // 根据业务修改
        deleteByIds(ids);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public AuthMenuDto insert(AuthMenuDto authMenuDto) {
        authMenuDto.setMenuId(UUIDGenerator.generateUUID());
        authMenuDto.setDelFlag(BaseConstant.NO);
        AuthMenu entity = BeanMapper.map(authMenuDto, AuthMenu. class);
        authMenuMapper.insert(entity);
        return authMenuDto;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public AuthMenuDto insertWithId(AuthMenuDto authMenuDto) {
        if (StringUtils.isBlank(authMenuDto.getMenuId())) {
            authMenuDto.setMenuId(UUIDGenerator.generateUUID());
        }
        authMenuDto.setDelFlag(BaseConstant.NO);
        AuthMenu entity = BeanMapper.map(authMenuDto, AuthMenu.class);
        authMenuMapper.insert(entity);
        return authMenuDto;
    }

    private List<AuthMenuDto> insertList(List<AuthMenuDto> authMenuDtos) {
        if (CollectionUtils.isEmpty(authMenuDtos)) {
            throw ExceptionFactory.createBaseException(BaseExceptionEnum.NOT_FOUND, "批量插入时，参数不能为空");
        }
        List<AuthMenu> insertList = new ArrayList<>();
        for (AuthMenuDto authMenuDto : authMenuDtos) {
            if (StringUtils.isBlank(authMenuDto.getMenuId())) {
                authMenuDto.setMenuId(UUIDGenerator.generateUUID());
            }
            authMenuDto.setDelFlag(BaseConstant.NO);
            insertList.add(BeanMapper.map(authMenuDto, AuthMenu.class));
        }
        authMenuMapper.insert(insertList, 1000);
        return authMenuDtos;
    }

    private List<AuthMenuDto> updateList(List<AuthMenuDto> authMenuDtos) {
        if (CollectionUtils.isEmpty(authMenuDtos)) {
            throw ExceptionFactory.createBaseException(BaseExceptionEnum.NOT_FOUND, "批量更新时，参数不能为空");
        }
        for (AuthMenuDto authMenuDto : authMenuDtos) {
            if (StringUtils.isBlank(authMenuDto.getMenuId())) {
                throw ExceptionFactory.createBaseException(BaseExceptionEnum.NOT_FOUND, "批量更新时，主键不能为空");
            }
        }
        List<AuthMenu> authMenuList = BeanMapper.mapList(authMenuDtos, AuthMenuDto.class, AuthMenu.class);
        authMenuMapper.updateById(authMenuList, 1000);
        return authMenuDtos;
    }

    @Transactional(rollbackFor = Exception.class)
    public int updateById(AuthMenuDto authMenuDto) {
        if (StringUtils.isBlank(authMenuDto.getMenuId())) {
            throw ExceptionFactory.createBaseException(BaseExceptionEnum.INTERNAL_SERVER_ERROR, "更新时，主键不能为空");
        }
        AuthMenu entity = BeanMapper.map(authMenuDto, AuthMenu.class);
        return authMenuMapper.updateById(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    public int deleteById(AuthMenuDto authMenuDto) {
        authMenuDto.setDelFlag(BaseConstant.YES);
        return updateById(authMenuDto);
    }
    
    @Transactional(rollbackFor = Exception.class)
    public void deleteByIds(List<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            throw ExceptionFactory.createBaseException(BaseExceptionEnum.NOT_FOUND, "批量删除时，参数不能为空");
        }
        for (String id : ids) {
            AuthMenuDto authMenuDto = new AuthMenuDto();
            authMenuDto.setMenuId(id);
            authMenuDto.setDelFlag(BaseConstant.YES);
            updateById(authMenuDto);
        }
    }

}
